package cn.com.miao.lynx.common.base.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BaseModel
 *
 * @title: BaseModel
 * @description:
 * @author: dengmiao
 * @create: 2019-12-23 16:24
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public abstract class BaseModel <T extends Serializable> implements Serializable {

    public final static int DELETE_FLAG_DELETE = 1;

    public final static int DELETE_FLAG_NORMAL = 0;

    /**
     * 主键
     * 非@org.springframework.data.annotation.Id
     * 而是@javax.persistence.Id
     * 默认生成策略根据数据库类型选择
     * 采用原生生成器代替TABLE生成器 相当于GenerationType.IDENTITY
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator="native")
    @GenericGenerator(name="native", strategy="native")
    @Column(name = "id")
    private T id;

    @CreatedBy
    @Column(name = "create_by", columnDefinition = "varchar(255) DEFAULT NULL COMMENT '创建人'")
    private String createBy;

    @LastModifiedBy
    @Column(name = "update_by", columnDefinition = "varchar(255) DEFAULT NULL COMMENT '更新人'")
    private String updateBy;

    @CreatedDate
    @Column(name = "create_time", columnDefinition = "datetime COMMENT '创建时间'")
    private LocalDateTime createTime;

    @LastModifiedDate
    @Column(name = "update_time", columnDefinition = "datetime COMMENT '更新时间'")
    private LocalDateTime updateTime;

    @Column(name = "del_status", nullable = false, columnDefinition = "int(1) DEFAULT 0 COMMENT '删除标识位 0正常 1已删除'", length = 1)
    private boolean delStatus;
}
